using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;

namespace THSERP.Modules.Department
{
    public partial class ICDepartmentAttributeValuesGridControl : THSGridControl
    {
        public override void InitGridControlDataSource()
        {
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            bds.DataSource = entity.ICDepartmentAttributeValuesList;
            this.DataSource = bds;
        }

        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            
            //Check add item privilege_Hieu.nguyen_29/04/2010
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            if (entity.Module.HasPrivilege("AddAttributeValue"))
            {
                gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
                gridView.InitNewRow += new InitNewRowEventHandler(GridView_InitNewRow);
            }
            //Check add item privilege_Hieu.nguyen_29/04/2010
            if (entity.Module.HasPrivilege("EditAttributeValue"))
            {
                //Set all column is enable
                foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
                {
                    column.OptionsColumn.AllowEdit = true;
                }
            }

            return gridView;
        }

        /// <summary>
        /// No. validation
        /// </summary>
        /// <remarks>Hieu.nguyen_06/04/2010</remarks>
        protected override void GridView_ValidatingEditor(object sender, DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs e)
        {
            base.GridView_ValidatingEditor(sender, e);

            if (e.Value == null || String.IsNullOrEmpty(e.Value.ToString()))
            {
                e.ErrorText = "Value can not be empty.";
                e.Valid = false;
                return;
            }
            GridView gridView = (GridView)sender;
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            THSList<ICDepartmentAttributeValuesInfo> lstAttribute = entity.lstICDeparmtneAttributeValues;
            foreach (ICDepartmentAttributeValuesInfo objDepartmentAttributeValuesInfo in lstAttribute)
            {
                DevExpress.XtraGrid.Columns.GridColumn col = gridView.FocusedColumn;
                if (lstAttribute.PosOf("ICDepartmentAttributeValueNo", e.Value) != -1 && col.FieldName == "ICDepartmentAttributeValueNo" && !gridView.FocusedValue.Equals(e.Value))
                {
                    e.ErrorText = "Attribute number can not be same as another.";
                    e.Valid = false;
                    return;
                }
            }
        }

        protected override void GridView_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            base.GridView_InitNewRow(sender, e);

            GridView gridView = (GridView)sender;
            DepartmentEntities entity = (DepartmentEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            String number = gridView.RowCount.ToString();
            while (number.Length < 2)
                number = '0' + number;
            if (String.IsNullOrEmpty(gridView.GetRowCellValue(e.RowHandle, "ICDepartmentAttributeValueNo").ToString()))
                gridView.SetRowCellValue(e.RowHandle, "ICDepartmentAttributeValueNo", number);
        }
    }
}
